/*
 *背景半透明处理
 *
 */
function colorToRGB(color) {

	var reg = /^#([0-9a-fA-f]{3}|[0-9a-fA-f]{6})$/g;

	var sColor = color.toLowerCase();

	if(sColor && reg.test(sColor)){

		if(sColor.length === 4){

			var sColorNew = "#";

			for(var i=1; i<4; i+=1){

				sColorNew += sColor.slice(i,i+1).concat(sColor.slice(i,i+1));

			}

			sColor = sColorNew;

		}

		//处理六位的颜色值

		var sColorChange = [];

		for(var i=1; i<7; i+=2){

			sColorChange.push(parseInt("0x"+sColor.slice(i,i+2)));

		}

		return sColorChange.join(",");

	}else{

		return color;

	}

}
function setBakckgoundAndOpacity(obj, color, opacity) {

	var rgbaObj = colorToRGB(color);

	if(jQuery.browser.msie && parseInt(jQuery.browser.version) < 9) {

		// 针对ie 6，7，8 通过设置内部元素position:relative；可以避免被父节点的opacity影响

		jQuery(obj).children().each(function(){

			if(jQuery(this).css('position') == 'static'){

				jQuery(this).css('position','relative');

			}

		});

		jQuery(obj).css({
	
			'background':color,
	
			'opacity':opacity
	
		});

	}else{

		// 支持rgba颜色格式的浏览器用rgba设置透明背景色

		jQuery(obj).css({

			'background':'rgba('+rgbaObj+','+opacity+')'

		});

	}
}

/**************************背景半透明处理***************************/

$(function() {
	/*setBakckgoundAndOpacity($('#navigationFont'),'#000000','0.3');
	setBakckgoundAndOpacity($('#loginFont'),'#000000','0.3');
	setBakckgoundAndOpacity($('#navigation'),'#000000','0.3');
	setBakckgoundAndOpacity($('#loginStorey'),'#000000','0.3');*/
	$("#loginButton").click(function(){
		doLogin();
	});
	doSetInputUserInfo();
});

/*
 * 图片滚动
 *@author liuhongjun
 */
$(function(){
	var $imgs = $(".imgSrcoll img");
	var width = $imgs.eq(0).width();
	var len = $imgs.length;
	var i = 0;
	var scrolling = setInterval(function(){
		if(i < (len - 1)){
			$imgs.eq(i).animate({left:"-"+width+"px"},1000);
			$imgs.eq(i+1).css({left:width+"px"}).animate({left:"0px",opacity:1},1000);
			i++;
		}else{
			$imgs.eq(len-1).animate({left:"-"+width+"px"},1000);
			$imgs.eq(0).css({left:width+"px"}).animate({left:"0px",opacity:1},1000);
			i = 0;
		}
			$(".num li").eq(i).addClass("on").siblings().removeClass("on");
		},4000);
		
	$(".num li").each(function(index){
		$(this).click(function(){
			if(i != index && index < len){
				$imgs.eq(i).animate({left:"-"+width+"px"},1000);
				$imgs.eq(index).css({left:width+"px"}).animate({left:"0px",opacity:1},1000);
				i = index;
			}
			$(".num li").eq(index).addClass("on").siblings().removeClass("on");
		});
	});
			
});
/**提交表单验证
 * @author liuhongjun
 */
function checkUserLoginForm(){
	if(!checkUserLoginName() || !checkUserLoginPassword()){
			return false;
	}
	return true;
}
/**用户登录名验证
* @author liuhongjun
 */
function checkUserLoginName(){
	var userLoginName = $("#userLoginName").val();
	if(isEmpty(userLoginName)){
		$("#errorLoginUserName").html("<font color='red'>"+ErrorMsgConstants.ERR_INPUT_IS_NOT_ALLOW_NULL+'</font>');
		return false;
	}
	if(!validateInput(userLoginName,3,30,"E")){
		$("#errorLoginUserName").html("<font color='red'>"+ErrorMsgConstants.ERR_USERID_FORMAT_IS_NOT_VALID+'</font>');
		return false;
	}
	/*$("#errorLoginUserName").html("<font color='green'>"+"√"+"</font>");*/
	return true;
}
/**用户登录密码验证
 * @author liuhongjun
 */
function checkUserLoginPassword(){
	var userLoginPassword = $("#userLoginPassword").val();
	if(isEmpty(userLoginPassword)){
		$("#errorLoginUserPassword").html("<font color='red'>"+ErrorMsgConstants.ERR_INPUT_IS_NOT_ALLOW_NULL+"</font>");
		return false;
		}
	if(!validateInput(userLoginPassword, 6, 20, "0aA")) {
		$("#errorLoginUserPassword").html("<font color='red'>"+ErrorMsgConstants.ERR_USERPASSWORD_FORMAT_IS_NOT_VALID+"</font>");
		$("#userLoginPassword").val("");
		return false;
		}
	/*$("#errorLoginUserPassword").html("<font color='green'>"+"√"+"</font>");*/
	return true;
}
/*
 * 登录ajaxLogin
 * */
function doLogin(){

	if(!checkUserLoginForm()){
		
		return false;
	}

	var userLoginName = $("#userLoginName").val();
	var userLoginPassword = $("#userLoginPassword").val();
	$("#loginButton").css("display", "none");
	/*$("#loadingImg").css("display", "block");*/
	$.ajax({
		type: 'post',
		url: '../user/ajaxLogin',
		data: {
			"email" : userLoginName,
			"password" : userLoginPassword
		},
		dataType: 'json',
		success: function(reultMap){
			if(reultMap.result == 0){
				closeLoginStorey();
				/*reload();*/
				/*showUser();*/
				if(doChange()){
					doInsertCookie(userLoginName,userLoginPassword);
				}
				var date = new Date();
				// 设置过期时间为半个小时
				date.setTime(date.getTime() + 30 * 60 * 1000);
				$.cookie("userHadLogin", true, {path: "/",expires: date});
				$("#loginButton").css("display", "block");
				alert("登陆成功");
				return true;
			}
			else {
				alert("登陆失败");
				$("#loginButton").css("display", "block");
				return false;
			}
			/*$("#loadingImg").css("display", "none");*/
		}
		
	});
}
function doInsertCookie(userLoginName,userLoginPassword){
	if($.cookie("userLoginName") != null){
		$.cookie("userLoginName",null);
	}
	if ($.cookie("userLoginPassword") != null) {
		$.cookie("userLoginPassword",null);
	}
	$.cookie("userLoginName",userLoginName,{expires:7});
	$.cookie("userLoginPassword",userLoginPassword,{expires:7});
}

function doSetInputUserInfo(){
	if($.cookie("userLoginName") != null && $.cookie("userLoginPassword") != null ){
		$("#userLoginName").val($.cookie("userLoginName"));
		$("#userLoginPassword").val($.cookie("userLoginPassword"));
	}
	
}
/*function showUser() {
	$("需要一个iframe,reload显示用户信息")
}*/
/*function reload(){
	window.location.reload(true);  
}*/
function doChange(){
	
	if(!$("#rember").attr("checked")){
		return true;
	}
	return false;
}
/*
 * 打开，关闭登录层
 * */
function openLoginStorey(){
	$('#loginStorey').css("display","block");
	$('.body_style').css('overflow-y','hidden');
}
function closeLoginStorey(){
	
	$('#loginStorey').css("display","none");
	$('.body_style').css('overflow-y','auto');
}